package com.shaw.study.springx.context.support;

import com.shaw.study.springx.annotation.Controller;
import com.shaw.study.springx.annotation.Service;
import com.shaw.study.springx.beans.BeanDefinition;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by asus on 2018/4/29.
 */
public class BeanDefinitionReader {

    private Properties config = new Properties();

    private final static String SCANPACKAGE="scanPackage";

    public BeanDefinitionReader(String[] configLocations){
        init(configLocations);
    }
    //定位配置文件
    public void init(String[] configLocations){
        try (InputStream inputStream = this.getClass()
                .getClassLoader().getResourceAsStream(configLocations[0].replaceAll("classpath:",""))){
            config.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册beanDefinitions
     * @param clazz
     * @return
     */
    public BeanDefinition doRegisterBeanDefinitions(Class clazz){
        BeanDefinition beanDefinition = new BeanDefinition();
        beanDefinition.setBeanClassName(clazz.getName());
        //beanName三种情况
        //1、默认类名首字母小写
        //2、注解中自定义名字
        //3、接口注入
        if (clazz.isAnnotationPresent(Controller.class)) {
            Controller controller = (Controller)clazz.getAnnotation(Controller.class);
            beanDefinition.setFactoryBeanName(getFactoryBeanName(controller.value(),clazz.getSimpleName()));
        }else if(clazz.isAnnotationPresent(Service.class)) {
            Service service = (Service)clazz.getAnnotation(Service.class);
            beanDefinition.setFactoryBeanName(getFactoryBeanName(service.value(),clazz.getSimpleName()));
        }
        return beanDefinition;
    }

    /**
     * \获取bean的名字
     * @param annotationValue
     * @param className
     * @return
     */
    private String getFactoryBeanName(String annotationValue,String className){
        String beanName;
        if(StringUtils.isEmpty(annotationValue)){
            beanName = getFirstLowerCase(className);
        }else{
            beanName = annotationValue;
        }
        return beanName;
    }

    //解析配置文件
    public List<String> praseProperties(){
        return doScan(config.getProperty(SCANPACKAGE));
    }

    public Properties getConfig(){
        return this.config;
    }

    /**
     * 解析配置文件
     */
    private List<String> doScan(String scanPackage) {
        List<String> classList = new ArrayList<>();

        URL url = BeanDefinition.class.getResource("/" + scanPackage.replaceAll("\\.","/"));

        File classDir = new File(url.getFile());

        for (File file : classDir.listFiles()){
            if(file.isDirectory()){
                classList.addAll(doScan(scanPackage + "." +file.getName()));
            }else {
                classList.add(scanPackage + "." + file.getName().replace(".class",""));
            }
        }

        return classList;
    }


    public String getFirstLowerCase(String str){
        char[] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }




}
